import axios from 'axios'
import { getTokenInfo } from '@/utils/publicFun'
import config from '@/utils/config'
const baseUrl = config.BASE_URL
const requestTimeout = config.REQUEST_TIMEOUT

// const token = ''
// sessionStorage.setItem('token', token)

/**
 * 创建axios实例
 */
const service = axios.create({
  baseURL: baseUrl,
  timeout: requestTimeout // 请求超时时间
})

/**
 * 请求拦截器
 */
service.interceptors.request.use(
  (config) => {
    config.headers = {
      'Content-Type': 'application/json;charset=UTF-8'
    }

    config.headers.userName = (() => {
      if (localStorage.userInfo) {
        return JSON.parse(localStorage.userInfo).userName
      } else {
        return ''
      }
    })()

    config.headers.userId = (() => {
      if (localStorage.userInfo) {
        return JSON.parse(localStorage.userInfo).userId
      } else {
        return ''
      }
    })()

    config.headers.token = (() => {
      if (getTokenInfo()) {
        return getTokenInfo()
      } else {
        // location.href = '/login'   // 这一步的作用是什么
        localStorage.removeItem('userInfo')
      }
    })()

    return config
  },
  (error) => Promise.error(error)
)

/**
 * 响应拦截器
 */
service.interceptors.response.use((res) => {
  return res
}, (err) => Promise.reject(err))

export function get(url, params, blob) {
  return new Promise((resolve, reject) => {
    service.get(url, {
      params
    }, blob).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.data)
    })
  })
}

export function post(url, params, blob) {
  return new Promise((resolve, reject) => {
    service.post(url, params, blob)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

export function otherPost(url, params, blob) {
  return new Promise((resolve, reject) => {
    service({
      method: 'POST',
      url,
      params,
      responseType: blob
    })
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

export function put(url, params) {
  return new Promise((resolve, reject) => {
    service.put(url, params)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

export function reqDelete(url, params) {
  return new Promise((resolve, reject) => {
    service.delete(url, { params })
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}

export function patch(url, params) {
  return new Promise((resolve, reject) => {
    service.patch(url, params)
      .then((res) => {
        resolve(res.data)
      })
      .catch((err) => {
        reject(err.data)
      })
  })
}
// 上传文件 进度处理
export function upData(url, params, callback) {
  return new Promise((resolve, reject) => {
    service.post(url, params, {
      onUploadProgress(progressEvent) {
        callback && callback(progressEvent)
      }
    }).then((res) => {
      resolve(res.data)
    }).catch((err) => {
      reject(err.data)
    })
  })
}

// /**
//  * post方法，对应post请求
//  * @param {String} url [请求的url地址]
//  * @param {Object} params [请求时携带的参数]
//  */
// export function postForm (url, params) {
//   return new Promise((resolve, reject) => {
//
//     service.post(url, Qs.stringify(params), { headers: {
//       'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' ,
//       "Ovo-Authorization": getAuthenticator()
//     } })
//       .then(res => {
//         resolve(res);
//       })
//       .catch(err => {
//         reject(err);
//       });
//   });
// }

export default service
